<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require(APPPATH.'/libraries/REST_Controller.php');

class Pay extends REST_Controller {
	public function __construct() {
		parent::__construct();
		$this->load->model('User_model');
		$this->load->library('session');
	}
	public function buy_goods_get() {
		if ((!$this->get('openid')) || (!$this->get('openkey')) ) {
			$this->response(array(
					'ret' => 1,
					'msg'=>'is not login',
			), 1);
		}
		else {
			$openid =  $this->get('openid');
			$openkey = $this->get('openkey');
		}
		//   	if(($this->session->userdata('id') == $openid)&&($this->session->userdata('session_id') == $openkey)){
		if(!$this->User_model->check_session_id($openid,$openkey)) {

			$this->response(array(
					'ret' => 1,
					'msg'=>'is not login',
			), 1);
		}else{
			if((!$this->get('itemid'))||(!$this->get('price'))||(!$this->get('ts'))) {
				$this->response(array(
						'ret' => 2,
						'msg'=>'missing parameter',
				), 1);
			}
			else {
				$itemid=$this->get('	');
				$price=$this->get('price');
				$ts=$this->get('ts');
				$sig=$this->get('sig');
			}
			
			// generate token_id, sig

			$token_id = hash('md5',time().uniqid());

			$this->load->model('Payment_model');
			//TODO: save to db
			$this->Payment_model->insert_new_payment_request($openid, $itemid, $price, $ts, $sig, $token_id);
			$this->response(array(
					'ret' => 0,
					'is_lost'=>'0',
					'url_params' => 'dev.innoflex.vn/apigame/api/pay/buy?openid='.$openid.'&openkey='.$openkey.'&token_id='.$token_id.'&sig='.$sig,
					'token' => 'default'
			), 1);
		}
	}
	public function buy_get () {
		//it is a JS API called by game web,or just jump to an other web page.
		// 		player use it to pass the num and uid to platform and pay.
		//request game server http://ip_of_game_server/cgi/vndeliver?
		//  openid=xxxxxxxxxxxxx&
		//  ts=1328855301&
		//  itemid=5001&
		//  num=1&
		//  uid=xxxxxx&
		//  token=4021A324754CCD7EA01836261D0AFF7207622&
		//  amt=80&
		//  sig=VvKwcaMqUNpKhx0XfCvOqPRiAnU%3D
		if ((!$this->get('openid')) || (!$this->get('openkey')) ) {
			$this->response(array(
					'ret' => 1,
					'msg'=>'is not login',
			), 1);
		}
		else {
			$openid =  $this->get('openid');
			$openkey = $this->get('openkey');
		}
		//   	if(($this->session->userdata('id') == $openid)&&($this->session->userdata('session_id') == $openkey)){
		if(!$this->User_model->check_session_id($openid,$openkey)) {
		
			$this->response(array(
					'ret' => 1,
					'msg'=>'is not login',
			), 1);
		}else {
			if ((!$this->get('token_id'))||(!$this->get('sig'))) {
				$this->response(array(
						'ret' => 2,
						'msg'=>'missing parameter',
				), 1);
			}
			else {
				$token_id = $this->get('token_id');
				$sig = $this->get('sig');
			}
			$this->load->model('Payment_model');
			$pay_request = $this->Payment_model->get_payment_by_token_id($token_id);
			if ($pay_request == null) {
				echo "Khong ton tai yeu cau";
			}
			$pay_request = $pay_request[0];
			$data=array();
			$data['user_id'] = $pay_request->user_id;
			$data['itemid'] = $pay_request->itemid;
			$data['price'] = $pay_request->price;
			$this->load->view('index');
		}
	}
}
// "ret":0,
// "is_lost":0,
// "url_params": "xxxxxxxxx?token_id=4021A324754CCD7EA01836261D0AFF7207622&sig=5b9feed5b43b8f8f829d19fb489814e4",
// "token": "4021A324754CCD7EA01836261D0AFF7207622"
// }